layui.use(['laydate','form','table','laytpl','layer'], function(){
	var laydate = layui.laydate;
	var form = layui.form;
	var table = layui.table;
	var laytpl = layui.laytpl;
	var layer = layui.layer;
	let type = $('.activity-btn').attr('lay-event')
	/*@初始化table*/
	layui.tableObj = table.render({
	    elem: '#table-list',
	    id: 'list',
	    url: '/message/message/index', //数据接口
	    page: true, //开启分页
	    where: {type: type},
	    cols: [[ //表头
	    	{type: 'checkbox'},
	    	{field: 'message_name', title: '短信模版'},
	    	{title: '操作', toolbar: '#table-btn'}
	    ]]
	});
	/*@按钮模版*/
	btn_temp = function(type){
		var getTpl_2 = oper_box.innerHTML;
		var count = document.getElementById('oper-box');
		laytpl(getTpl_2).render(type, function(html){
			count.innerHTML = html;
		});
	}
	btn_temp(type);
	/*@删除请求*/
	del = function(obj_data){
		$.get('/message/message/del', obj_data, function(data){
			if(data.code !== 1){
				layer.msg(data.msg, {icon: 5})
			}else{
				layer.msg(data.msg, {icon: 6, time: 1000}, function(){
					layui.tableObj.reload()
				})
			}
		})
	}
	/*@批量删除*/
	del_all = function(){
		var checkStatus = table.checkStatus('list')
	    var data = checkStatus.data;
	    if(data.length == 0){
	    	layer.msg('请选中要删除的数据',{icon: 5})
	    	return
	    }
	    let obj = {}
	    obj.id = []
	    for(var i = 0;i < data.length;++i){
	    	obj.id.push(data[i].message_id)
	    }
	    del(obj)
	}
	/*@监听工具*/
	table.on('tool(list)', function(obj_tr){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
	  	var data = obj_tr.data; //获得当前行数据
	  	var layEvent = obj_tr.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
	  	var tr = obj_tr.tr; //获得当前行 tr 的DOM对象
	  	if(layEvent === 'del'){ //删除
	  		let obj_data = {}
	  		obj_data.id = data.message_id
	  		del(obj_data)
	  	}
	});
	/*@查询*/
	form.on('submit(search)',function(data){
		$(data.elem).addClass('activity-btn');
		$(data.elem).removeClass('box-btn');
		$(data.elem).siblings().addClass('box-btn');
		$(data.elem).siblings().removeClass('activity-btn');
		let type = $(data.elem).attr('lay-event');
		btn_temp(type);
		/*重置表格数据*/
		layui.tableObj.reload({
			page: {
                page: 1 //重新从第 1 页开始
            },
            where: {type: type}
        });
        return false
	})
	
});